package io.finer.erp.jeecg.bas.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.finer.erp.jeecg.bas.entity.BasPrinter;
import io.finer.erp.jeecg.bas.service.IBasPrinterService;
import io.finer.erp.jeecg.stock.service.IStkInventoryService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.jeecg.common.system.base.controller.JeecgController;
import org.jeecg.common.system.query.QueryGenerator;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Arrays;

 /**
 * @Description: 打印机管理
 * @Author: jeecg-boot
 * @Date:   2021-03-25
 * @Version: V1.0
 */
@Api(tags="打印机管理")
@RestController
@RequestMapping("/bas/basPrinter")
@Slf4j
public class BasPrinterController extends JeecgController<BasPrinter, IBasPrinterService> {
	@Autowired
	private IBasPrinterService basPrinterService;
	@Autowired
	private IStkInventoryService stkInventoryService;
	
	/**
	 * 分页列表查询
	 *
	 * @param basPrinter
	 * @param pageNo
	 * @param pageSize
	 * @param req
	 * @return
	 */
	@AutoLog(value = "打印机管理-分页列表查询")
	@ApiOperation(value="打印机管理-分页列表查询", notes="打印机管理-分页列表查询")
	@GetMapping(value = "/list")
	public Result<?> queryPageList(BasPrinter basPrinter,
								   @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
								   @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
								   HttpServletRequest req) {
		QueryWrapper<BasPrinter> queryWrapper = QueryGenerator.initQueryWrapper(basPrinter, req.getParameterMap());
		Page<BasPrinter> page = new Page<BasPrinter>(pageNo, pageSize);
		IPage<BasPrinter> pageList = basPrinterService.page(page, queryWrapper);
		return Result.ok(pageList);
	}
	
	/**
	 *   添加
	 *
	 * @param basPrinter
	 * @return
	 */
	@AutoLog(value = "打印机管理-添加")
	@ApiOperation(value="打印机管理-添加", notes="打印机管理-添加")
	@PostMapping(value = "/add")
	public Result<?> add(@RequestBody BasPrinter basPrinter) {
		basPrinter.setDelFlag(0);
		basPrinter.setStatus(0);
		basPrinterService.save(basPrinter);
		return Result.ok("添加成功！");
	}
	
	/**
	 *  编辑
	 *
	 * @param basPrinter
	 * @return
	 */
	@AutoLog(value = "打印机管理-编辑")
	@ApiOperation(value="打印机管理-编辑", notes="打印机管理-编辑")
	@PutMapping(value = "/edit")
	public Result<?> edit(@RequestBody BasPrinter basPrinter) {
		basPrinter.setStatus(0);
		basPrinterService.updateById(basPrinter);
		return Result.ok("编辑成功!");
	}

	 /**
	  *  绑定
	  *
	  * @param basPrinter
	  * @return
	  */
	 @AutoLog(value = "打印机管理-绑定")
	 @ApiOperation(value="打印机管理-绑定", notes="打印机管理-绑定")
	 @PutMapping(value = "/bindPrinter")
	 public Result<?> bindPrinter(@RequestBody BasPrinter basPrinter) {
		 try {
			 basPrinterService.bindPrinter(basPrinter);
		 } catch (Exception e) {
			 return Result.error("绑定失败!"+e.getMessage());
		 }
		 return Result.ok("绑定成功!");
	 }
	
	/**
	 *   通过id删除
	 *
	 * @param id
	 * @return
	 */
	@AutoLog(value = "打印机管理-通过id删除")
	@ApiOperation(value="打印机管理-通过id删除", notes="打印机管理-通过id删除")
	@DeleteMapping(value = "/delete")
	public Result<?> delete(@RequestParam(name="id",required=true) String id) {
		basPrinterService.removeById(id);
		return Result.ok("删除成功!");
	}
	
	/**
	 *  批量删除
	 *
	 * @param ids
	 * @return
	 */
	@AutoLog(value = "打印机管理-批量删除")
	@ApiOperation(value="打印机管理-批量删除", notes="打印机管理-批量删除")
	@DeleteMapping(value = "/deleteBatch")
	public Result<?> deleteBatch(@RequestParam(name="ids",required=true) String ids) {
		this.basPrinterService.removeByIds(Arrays.asList(ids.split(",")));
		return Result.ok("批量删除成功!");
	}
	
	/**
	 * 通过id查询
	 *
	 * @param id
	 * @return
	 */
	@AutoLog(value = "打印机管理-通过id查询")
	@ApiOperation(value="打印机管理-通过id查询", notes="打印机管理-通过id查询")
	@GetMapping(value = "/queryById")
	public Result<?> queryById(@RequestParam(name="id",required=true) String id) {
		BasPrinter basPrinter = basPrinterService.getById(id);
		if(basPrinter==null) {
			return Result.error("未找到对应数据");
		}
		return Result.ok(basPrinter);
	}

    /**
    * 导出excel
    *
    * @param request
    * @param basPrinter
    */
    @RequestMapping(value = "/exportXls")
    public ModelAndView exportXls(HttpServletRequest request, BasPrinter basPrinter) {
        return super.exportXls(request, basPrinter, BasPrinter.class, "打印机管理");
    }

    /**
      * 通过excel导入数据
    *
    * @param request
    * @param response
    * @return
    */
    @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
    public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
        return super.importExcel(request, response, BasPrinter.class);
    }

     @ApiOperation(value="打印机管理-测试", notes="打印机管理-测试")
     @GetMapping(value = "/test")
    public Result<?> test(HttpServletRequest request, HttpServletResponse response,String id){
//        stkInventoryService.upDdFile(id);
        return Result.ok();
    }

}
